/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.View;

import com.lcfgly.Database.ShopArchives;
import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.Message;
import com.lcfgly.Form.Validation.ArchivesValidation;
import com.lcfgly.Interface.UserSession;
import com.lcfgly.Service.ArchivesService;
import com.lcfgly.Service.UserMenuService;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 文章
 * @author lcfgly
 */
@Controller
@RequestMapping("/control")
public class ArchivesControl extends FatherController{
    
    @Autowired
    UserMenuService UserMenuService;
    
    @Autowired
    ArchivesService ArchivesService;
    
    /**
     * 添加文章页
     * @param id
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/archivesadd/{type}",method = RequestMethod.GET)
    public ModelAndView archive(@PathVariable("type") int id,@UserSession UserInfo session)throws ActionException{
            return new ModelAndView("controller/archiveadd","typeinfo",UserMenuService.FindSimpleByIdAndUserId(session.getUid(), id));
     }
    
    /**
     * 添加文章
     * @param archives
     * @param res
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/archivesadd/{type}",method = RequestMethod.POST)
    public ModelAndView archive(@Validated ShopArchives archives,BindingResult res,@UserSession UserInfo session) throws ActionException{
        if(res.hasFieldErrors()){
            ModelAndView result=archive(archives.getTypeid(),session);
            return result.addObject("error", getError(res));
        }
        archives.setWriter(session.getUsername());
        archives.setWriteruid(session.getUid());
        ArchivesService.create(archives);
        throw new Message("发布成功");
    }
    
    /**
     * 文章列表页
     * @param id
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/archives/{type}",method = RequestMethod.GET)
    public ModelAndView archiveList(@PathVariable("type") int id,@UserSession UserInfo session)throws ActionException{
            return new ModelAndView("controller/archives","typeinfo",UserMenuService.FindSimpleByIdAndUserId(session.getUid(), id));
    }
    
    /**
     * 文章列表json数据
     * @param request
     * @param typeid
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/archivelist/{typeid}",method = RequestMethod.POST)
    @ResponseBody
    public Map archiveList(HttpServletRequest request,@PathVariable("typeid") int typeid,@UserSession UserInfo session)throws ActionException
    {
           return ArchivesService.GetMenuListForDataTable(request, typeid, session.getUid());
    }
    
    /**
     * 修改文章页
     * @param id
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value="/archivesedit/{id}",method = RequestMethod.GET)
    public ModelAndView archiveEdit(@PathVariable("id") int id,@UserSession UserInfo session) throws ActionException{
        ShopArchives archives=ArchivesService.findByIdAndUserid(id, session.getUid());
        ModelAndView result=new ModelAndView("controller/archiveedit","info",archives);
        return result.addObject("typeinfo", UserMenuService.FindSimpleByIdAndUserId(session.getUid(), archives.getTypeid()));
    }
    
    /**
     * 修改文章
     * @param archives
     * @param res
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value="/archivesedit/{id}",method = RequestMethod.POST)
    public ModelAndView archiveEdit(@Validated ShopArchives archives,BindingResult res,@UserSession UserInfo session) throws ActionException{
        if(res.hasFieldErrors()){
            ModelAndView result=archiveEdit(archives.getId(),session);
            return result.addObject("error", getError(res));
        }
        archives.setWriter(session.getUsername());
        archives.setWriteruid(session.getUid());
        ArchivesService.save(archives);
        throw new Message("修改成功");
    }
    
    /**
     * 数据验证配置
     * @param binder 
     */
    @InitBinder
    public void initValidation(WebDataBinder binder){
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CustomDateEditor dateEditor=new CustomDateEditor(df, true);
        binder.registerCustomEditor(Date.class, dateEditor);
        binder.setValidator(new ArchivesValidation());
    }
    
    /**
     * 删除文章
     * @param id
     * @param session
     * @return 
     */
   @RequestMapping(value = "/archives/del",method = RequestMethod.POST)
   @ResponseBody
    public Object archiveDel(@RequestParam("id") int id,@UserSession UserInfo session){
        return ArchivesService.del(id, session.getUid());
    }
    
}
